package com.ruoyi.channel.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.ApplicationInfo;
import com.ruoyi.db.mapper.ApplicationInfoMapper;
import com.ruoyi.laiqianba.constant.RedisKeyConstant;
import com.ruoyi.laiqianba.service.IHandleCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.ChannelBaseInfoMapper;
import com.ruoyi.db.domain.ChannelBaseInfo;
import com.ruoyi.channel.service.IChannelBaseInfoService;

import javax.annotation.Resource;

/**
 * 渠道基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-01
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class ChannelBaseInfoServiceImpl implements IChannelBaseInfoService {
    @Resource
    private ChannelBaseInfoMapper channelBaseInfoMapper;
    @Resource
    private ApplicationInfoMapper applicationInfoMapper;
    @Autowired
    private IHandleCacheService handleCacheService;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 查询渠道基本信息
     *
     * @param id 渠道基本信息主键
     * @return 渠道基本信息
     */
    @Override
    public ChannelBaseInfo selectChannelBaseInfoById(Long id) {
        return channelBaseInfoMapper.selectChannelBaseInfoById(id);
    }

    @Override
    public List<Map<String, String>> getChannelNameList() {
        return channelBaseInfoMapper.getChannelNameList();
    }
    @Override
    public List<Map<String, String>> getChannelNameList1() {
        return channelBaseInfoMapper.getChannelNameList1();
    }

    @Override
    public List<Map<String, String>> getChannelNameListOpen() {
        return channelBaseInfoMapper.getChannelNameListOpen();
    }

    /**
     * 查询渠道基本信息列表
     *
     * @param channelBaseInfo 渠道基本信息
     * @return 渠道基本信息
     */
    @Override
    public List<ChannelBaseInfo> selectChannelBaseInfoList(ChannelBaseInfo channelBaseInfo) {
        return channelBaseInfoMapper.selectChannelBaseInfoList(channelBaseInfo);
    }

    /**
     * 查询渠道基本信息列表
     *
     * @param channelBaseInfo 渠道基本信息
     * @return 渠道基本信息
     */
    @Override
    public List<Map<String, Object>> selectChannelBaseInfoMapList(ChannelBaseInfo channelBaseInfo) {
        return channelBaseInfoMapper.selectChannelBaseInfoMapList(channelBaseInfo);
    }

    /**
     * 新增渠道基本信息
     *
     * @param channelBaseInfo 渠道基本信息
     * @return 结果
     */
    @Override
    public int insertChannelBaseInfo(ChannelBaseInfo channelBaseInfo) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        channelBaseInfo.setOwnerId(loginUser.getUserId());
        channelBaseInfo.setOwnerName(loginUser.getUser().getNickName());

        ApplicationInfo applicationInfo = applicationInfoMapper.selectApplicationInfoByAppCode(channelBaseInfo.getAppCode());
        String pageUrl = StrUtil.format(applicationInfo.getExtUrl(), channelBaseInfo.getChannelCode());
        channelBaseInfo.setPageUrl(pageUrl);
        channelBaseInfo.setAppName(applicationInfo.getAppName());

        channelBaseInfo.setCreateTime(DateUtils.getNowDate());
        return channelBaseInfoMapper.insertChannelBaseInfo(channelBaseInfo);
    }

    /**
     * 修改渠道基本信息
     *
     * @param channelBaseInfo 渠道基本信息
     * @return 结果
     */
    @Override
    public int updateChannelBaseInfo(ChannelBaseInfo channelBaseInfo) {
        ChannelBaseInfo baseInfo = this.channelBaseInfoMapper.selectChannelBaseInfoById(channelBaseInfo.getId());

        if (!StrUtil.equals(baseInfo.getAppCode(), channelBaseInfo.getAppCode())) {
            ApplicationInfo applicationInfo = applicationInfoMapper.selectApplicationInfoByAppCode(channelBaseInfo.getAppCode());
            String pageUrl = StrUtil.format(applicationInfo.getExtUrl(), channelBaseInfo.getChannelCode());
            channelBaseInfo.setPageUrl(pageUrl);
            channelBaseInfo.setAppName(applicationInfo.getAppName());
        }

        channelBaseInfo.setUpdateTime(DateUtils.getNowDate());
        int i = channelBaseInfoMapper.updateChannelBaseInfo(channelBaseInfo);

        if (i > 0) {
            handleCacheService.clearChannelCache(baseInfo.getChannelCode());
        }

        String redisKey = StrUtil.format(RedisKeyConstant.CHANNEL_CACHE_KEY, baseInfo.getChannelCode());

        if (i > 0) {
            redisTemplate.delete(redisKey);
        }

        return i;
    }

    @Override
    public int updateSmsStatus(ChannelBaseInfo channelBaseInfo) {

        int i = channelBaseInfoMapper.updateChannelBaseInfo(channelBaseInfo);

        ChannelBaseInfo baseInfo = this.channelBaseInfoMapper.selectChannelBaseInfoById(channelBaseInfo.getId());

        if (i > 0) {
            handleCacheService.clearChannelCache(baseInfo.getChannelCode());
        }

        String redisKey = StrUtil.format(RedisKeyConstant.CHANNEL_CACHE_KEY, baseInfo.getChannelCode());

        if (i > 0) {
            redisTemplate.delete(redisKey);
        }
        return i;
    }


    /**
     * 删除渠道基本信息信息
     *
     * @param id 渠道基本信息主键
     * @return 结果
     */
    @Override
    public int deleteChannelBaseInfoById(Long id) {
        ChannelBaseInfo channelBaseInfo = new ChannelBaseInfo();
        channelBaseInfo.setId(id);
        channelBaseInfo.setStatus(0L);
        channelBaseInfo.setDelFlag(1L);
        ChannelBaseInfo baseInfo = this.channelBaseInfoMapper.selectChannelBaseInfoById(id);

        String redisKey = StrUtil.format(RedisKeyConstant.CHANNEL_CACHE_KEY, baseInfo.getChannelCode());

        int i = channelBaseInfoMapper.deleteChannelById(id);
        if (i > 0) {
            redisTemplate.delete(redisKey);
        }
        return i;
    }

    @Override
    public List<Map<String, String>> getChannelNameList2() {
        return channelBaseInfoMapper.getChannelNameList2();
    }

    @Override
    public int updateList() {
        return channelBaseInfoMapper.updateList();
    }

    @Override
    public int updateAuth() {
        return channelBaseInfoMapper.updateAuth();
    }

    @Override
    public int updateChannelBaseInfoByTask(List<String> appCodes,ChannelBaseInfo channelBaseInfo) {
        return channelBaseInfoMapper.updateChannelBaseInfoByTask(appCodes,channelBaseInfo);
    }

    @Override
    public int  updateChannelBaseInfoByTask1(List<String> appCodes,ChannelBaseInfo channelBaseInfo) {
        return channelBaseInfoMapper.updateChannelBaseInfoByTask1(appCodes,channelBaseInfo);
    }

    @Override
    public List<Map<String, String>> getChannelNameList3() {
        return channelBaseInfoMapper.getChannelNameList3();
    }

    @Override
    public List<Map<String, String>> getChannelNameList4() {
        return channelBaseInfoMapper.getChannelNameList4();
    }
}
